<?php
namespace app\api\controller;
use think\Db;
use think\facade\{
    View,Env,Config
};
class Docs
{
    public $view;
    public function __construct(){
        $this->view = View::init([
            'tpl_begin'    => '<{',
            'tpl_end'      => '}>',
            'taglib_begin' => '<{',
            'taglib_end'   => '}>',
            'layout_on'     =>  false,
        ]);
        Config::set('default_return_type','html');
    }
    /**
     * 调试工具
     * @param string $app 项目配置
     */
    public function index()
    {
        $nodes = $this->nodes();
        $token = Db::name('user')->order('id asc')->value('token');
        $d = ['nodes'=>$nodes, 'token'=>$token];
        return $this->view->fetch('',$d);
    }
    //加密
    public function jiami(){
        if(is_post()){
            if(input('type')){
                return decode(input('title'));
            }else{
                return encode(input('title'));
            }
        }
        return $this->view->fetch();
    }

    //获取
    public function getsign(){
        if(is_post()){
            $d = input();
            return sign($d,$d['app_key']);
        }
    }


    //节点
    private function nodes(){
        $base_url = "http://api.".Env::get('domain');

        $file = scandir(dirname(__FILE__)."/../docsconfig/");
        unset($file[0]);
        unset($file[1]);
        $temp = [];
        foreach($file as $k => $f){
            $temp[$k] = include dirname(__FILE__)."/../docsconfig/".$f;
        }

        //组装前端需要的格式
        $nodes = [];
        foreach($temp as $val){
            $arr = ["name" => $val['name'],];
            foreach($val['list'] as $k => $v){
                $arr["children"][] = [
                    "name" => $k,
                    "data" => $v,
                ];
            }
            $nodes[] = $arr;
        }
        return json_encode($nodes,JSON_UNESCAPED_UNICODE);
    }

    //上传
    public function up(){
        return $this->view->fetch();
    }
}
